package cn.swufe.edu.stu.hw_tools.Cal;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import cn.swufe.edu.stu.hw_tools.R;

public class BmiActivity extends AppCompatActivity {
    private Button backbtn,getbtn;
    private TextView v1,v2,v3;
    private double height,length,ans;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bmi);

        backbtn = findViewById(R.id.bmibackbutton);
        getbtn = findViewById(R.id.getbmibutton);
        v1 = findViewById(R.id.BMIheight);
        v2 = findViewById(R.id.BMIlength);
        v3 = findViewById(R.id.BMIans);

        backbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = getIntent();
                setResult(2,intent);
                finish();
            }
        });
        getbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean flag = true;
                try {
                    String text1 = v1.getText().toString();
                    double number1 = Double.parseDouble(text1);
                    String formattedString1 = String.format("%.2f", number1);
                    height = Double.parseDouble(formattedString1);
                } catch (Exception e){
                    flag = false;
                    Toast.makeText(BmiActivity.this, "( つ•̀ω•́)つ输一下体重，兄弟！", Toast.LENGTH_SHORT).show();
                }
                try {
                    String text2 = v2.getText().toString();
                    double number2 = Double.parseDouble(text2);
                    String formattedString2 = String.format("%.2f", number2);
                    length = Double.parseDouble(formattedString2);
                } catch (Exception e){
                    flag = false;
                    Toast.makeText(BmiActivity.this, "( つ•̀ω•́)つ输一下身高，兄弟！", Toast.LENGTH_SHORT).show();
                }
                if (flag) {
                    ans = height/length/length;
                    String advice;
                    if(ans<18.5){
                        advice = "\n您的体重偏轻";
                    }else if(ans>25){
                        advice = "\n您的体重偏重";
                    }else{
                        advice = "\n您的体重属于标准体重";
                    }
                    String showans = String.format("%.2f", ans);
                    v3.setText(showans+advice);
                }
            }
        });
    }
}